import QtQuick 2.1
import QtQuick.Window 2.0
import QtQuick.Controls 1.1
//import an.qt.ColorMaker 1.0

Window {
    visible: true
    width: 360
    height: 360

    Rectangle {
        visible: true
        width: 360
        height: 360

        Text {
            id: timeLabel;
            anchors.left: parent.left;
            anchors.leftMargin: 4;
            anchors.top: parent.top;
            anchors.topMargin: 4;
            font.pixelSize: 26;
        }

//        ColorMaker {
//            id: colorMaker;
//            color: Qt.green;
//        }

        Rectangle {
            id: colorRect;
            anchors.centerIn: parent;
            width: 200;
            height: 200;
            color: "blue";
        }

        Button {
            id: start;
            text: qsTr("Start");
            anchors.left: parent.left;
            anchors.leftMargin: 4;
            anchors.bottom: parent.bottom;
            anchors.bottomMargin: 4;
            onClicked: {
                colorMaker.start();
            }
        }

        Button {
            id: stop;
            text: qsTr("Stop");
            anchors.left: start.right;
            anchors.leftMargin: 4;
            anchors.bottom: start.bottom;
            onClicked: {
                colorMaker.stop();
            }
        }

        function changeAlgorithm(button, algorithm) {
            switch(algorithm) {
            case 0:
                button.text = "RandomRGB";
                break;
            case 1:
                button.text = "RandomRed";
                break;
            case 2:
                button.text = "RandomGreen";
                break;
            case 3:
                button.text = "RandomBlue";
                break;
            case 4:
                button.text = "LinearIncrease";
                break;
            }
        }

        Button {
            id: colorAlgorithm;
            text: qsTr("RandomRGB");
            anchors.left: stop.right;
            anchors.leftMargin: 4;
            anchors.bottom: start.bottom;
            onClicked: {
                var algorithm = (colorMaker.algorithm()+1)%5;
                parent.changeAlgorithm(colorAlgorithm, algorithm);
                colorMaker.set_algorithm(algorithm);
            }
        }

        Button {
            id: quit;
            text: qsTr("Quit");
            anchors.left: colorAlgorithm.right;
            anchors.leftMargin: 4;
            anchors.bottom: start.bottom;
            onClicked: {
                Qt.quit();
            }
        }

        /* 根元素创建完成时，执行该事件 */
        Component.onCompleted: {
            colorMaker.color = Qt.rgba(0, 180, 128, 255)
            colorMaker.set_algorithm(2);
            changeAlgorithm(colorAlgorithm, colorMaker.algorithm())
        }

        Connections {
            target: colorMaker;
            onCurrent_time: {
                timeLabel.text = time;
                timeLabel.color = colorMaker.timeColor;
            }
        }

        Connections {
            target: colorMaker;
            onColor_changed: {
                colorRect.color = color;
            }
        }
    }
}
